<?php
/**
 * admin 学生管理
 */
class Admin_student extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        //登陆状态检测
        if (!$this->session->userdata('r_user_in')) {
            redirect('home');
        }
        //权限检测
        if ($this->session->userdata('r_role_id') != 1) {
            redirect('home');
        }
    }

    /**
     * student管理的主界面
     * @return [type]
     */
    public function index()
    {
        //准备数据
        $data = array();
        $data['title'] = '学生管理';

        $this->load->model('student_model');
        $this->load->model('major_model');
        $this->load->model('logic_class_model');
        $this->load->library('form_validation');
    
        // $data['students'] = $this->student_model->get_all_students();
        $students = $this->student_model->get_all_students();
        //var_dump($this->student_model->get_all_students());
        $data['majors'] = $this->major_model->get_all();
        $data['logic_classes'] = $this->logic_class_model->get_all();
        $data['students'] = $students;
        //填充查询默认数据
        $data['major_id_select'] = 0;
        $data['logic_class_id_select'] = 0;
        $data['student_type_select'] = 0;

        $this->load->view('admin_student/index', $data);
    }

    /**
     * 添加学生
     */
    public function add()
    {
        //表单验证
        // if($this->form_validation->run('admin_student/add'))
        // {
        // 	//post方法
        // 	//读取数据
        // 	$student_name=$this->input->post('student_name');
        // 	$student_id=$this->input->post('student_id');
        // 	$logic_classes=$this->input->post('logic_classes');
        // 	echo("11111111111");
        // 	//$student_type=$this->input->post('student_type');
        // 	//新建一个用户
        // 	$user = array(
        // 		'username' => $student_id,
        // 		'password' => md5($student_id),
        // 		'role_id' => 3,
        // 		 );

        // 	$this->load->model('user_model');
        // 	if($this->user_model->add($user))
        // 	{
        // 		$user_id=$this->db->insert_id();

        // 		//将学生写入数据库
        // 		$student = array(
        // 			'user_id' => $user_id,
        // 			'name' => $student_name,
        // 			'student_id' => $student_id,
        // 			'major_id' => 4,
        // 			'type' => 1,
        // 			'school_id' => 7,
        // 			 );
        // 		$this->load->model('student_model');
        // 		if($this->student_model->add($student))
        // 		{
        // 			//添加学生选课关系
        // 			$student_id=$this->db->insert_id();
        // 			if($this->student_model->add_relative_logic_classes($student_id,$logic_classes))
        // 			{
        // 				$this->session->set_flashdata('message','添加成功');
        // 				redirect('admin_student/index');
        // 			}
                    
        // 		}
        // 	}
        // 	$this->session->set_flashdata('message','操作失败');
        // 	redirect('admin_student/index');
        // }
        //get方法
        $data['title']='添加学生';
        $this->load->model('major_model');
        $this->load->model('logic_class_model');
        $data['majors']=$this->major_model->get_all();
        $data['logic_classes']=$this->logic_class_model->get_all();
        $this->load->view('admin_student/add', $data);
    }


    public function addStudent()
    {
    
        //读取数据
        $student_name=$this->input->post('student_name');
        $student_id=$this->input->post('student_id');
        $logic_classes=$this->input->post('logic_classes');
        //$student_type=$this->input->post('student_type');
        //新建一个用户
        $user = array(
            'username' => $student_id,
            'password' => md5($student_id),
            'role_id' => 3,
             );

        $this->load->model('user_model');
        if ($this->user_model->add($user)) {
            $user_id=$this->db->insert_id();
            echo $user["username"];

            //将学生写入数据库
            $student = array(
              'user_id' => $user_id,
              'name' => $student_name,
              'student_id' => $student_id,
              'major_id' => 4,
              'type' => 1,
              'school_id' => 7,
                 );
            $this->load->model('student_model');
            if ($this->student_model->add($student)) {
                //添加学生选课关系
                $student_id=$this->db->insert_id();
                if ($this->student_model->add_relative_logic_classes($student_id, $logic_classes)) {
                    $this->session->set_flashdata('message', '添加成功');
                    redirect('admin_student/index');
                }
            }
        }

        $this->session->set_flashdata('message', '操作失败');
        redirect('admin_student/index');
    }
    /**
     * 批量删除学生
     */
    public function batch_delete()
    {
        $students_id = $this->input->post('batch_delete_students');
        var_dump($students_id);
        //计数
        $students_num = 0;
        $students_delete = 0;

        $this->load->model('student_model');
        foreach ($students_id as $students_id_item) {
            if (!$this->student_model->get_by_id($students_id_item)) {
                $this->load->view('error');
            }
            $students_num++;
            if ($this->student_model->delete_cascade($students_id_item)) {
                $students_delete++;
            }
        }

        if ($students_num == $students_delete) {
            $this->session->set_flashdata('message', '操作成功');
        } else {
            $this->session->set_flashdata('message', '存在相关引用，未完全删除');
        }

        redirect('admin_student/index');
    }
    /**
     * 批量添加学生
     */
    public function batch_add()
    {
        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'text|txt';
        $config['max_size'] = '1000';
        $config['file_name'] = date('ymdHis', time());
        $this->load->library('upload', $config);
        if (!$this->upload->do_upload('student_file')) {
            $data['error_message'] = '文件不符合要求';
            $this->load->view('error', $data);
        } else {
            $file_info = $this->upload->data();
            $content = file_get_contents($file_info['full_path']);
            $content = $this->remove_utf8_bom($content);
            $first_line = strtok($content, "\n");
            $second_line = strtok("\n");
            $third_line = strtok("\n");
            $item = strtok("\n");
            $items = array();
            while ($item) {
                array_push($items, $item);
                $item = strtok("\n");
            }

            $teacher_name = strtok($first_line, ",");
            $teacher_name = substr($teacher_name, 1);
            $teacher_name = trim($teacher_name);
            $teacher_number = strtok("\n");
            $teacher_number = trim($teacher_number);
            $this->load->model('teacher_model');
            $teacher_exist = $this->teacher_model->teacher_number_exist($teacher_number);
            $teacher_state = false;
            if (!$teacher_exist) {
                $teacher_state = true;
            } else {
                $teacher = $this->teacher_model->get_by_teacher_number($teacher_number);
                if ($teacher_name == $teacher['name']) {
                    $teacher_state = true;
                }
            }

            $me = $this->teacher_model->get_by_username($this->session->userdata('r_username'));
            if (!$teacher_exist) {
                $teacher_state = false;
            } else {
                $teacher = $this->teacher_model->get_by_teacher_number($teacher_number);
            
                // if ($teacher_name == $teacher['name'] && $teacher['teacher_number'] == $me['teacher_number'])
                // 	$teacher_state = true;
                if ($teacher_name == $teacher['name'] && $teacher['teacher_number']) {
                    $teacher_state = true;
                }
            }
            $logic_class_name = strtok($second_line, ",");
            $logic_class_name = substr($logic_class_name, 1);
            $logic_class_name = trim($logic_class_name);
            // $big_lecture_name = strtok($second_line, ",");
            // $big_lecture_name = substr($big_lecture_name, 1);
            // $big_lecture_name = trim($big_lecture_name);
            // $logic_class_name = strtok("\n");
            // $logic_class_name = trim($logic_class_name);
            $this->load->model('logic_class_model');
            $this->load->model('big_lecture_model');
            $big_lecture_name = "电工技术";
            $logic_class_exist = $this->logic_class_model->logic_class_number_exist($logic_class_name);

            $logic_class_state = false;
            $this->load->model('student_model');
            $students = array();
            foreach ($items as $item) {
                $student['student_id'] = strtok($item, ',');
                $student['student_id'] = trim($student['student_id']);
                $student['student_name'] = strtok("\n");
                $student['student_name'] = trim($student['student_name']);
                $student['student_exist'] = $this->student_model->student_id_exist($student['student_id']);

                $student['student_state'] = false;
                if (!$student['student_exist']) {
                    $student['student_state'] = true;
                } else {
                    $stu = $this->student_model->get_by_student_id($student['student_id']);
                    if ($student['student_name'] == $stu['name']) {
                        $student['student_state'] = true;
                    }
                }
                if (!($student['student_id'] && $student['student_name'])) {
                    $data['error_message'] = '文件信息不完整，或格式错误1';
                    $this->load->view('error', $data);
                }
                array_push($students, $student);
            }

            $this->load->model('logic_class_model');
            $logic_class_exist = $this->logic_class_model->logic_class_number_exist($logic_class_name);
            $logic_class_state = false;
            //检查课程名称是否存在
            $this->load->model('big_lecture_model');
            $lecture_exist = $this->big_lecture_model->check_lecture_exist($big_lecture_name);
            if (!$lecture_exist) {
                $logic_class_state = false;
            } else {
                if (!$logic_class_exist) {
                    $logic_class_state = true;
                } else {
                    // $logic_class = $this->logic_class_model->get_by_logic_class_number($logic_class_name);
                    // if ($logic_class_name == $logic_class['big_lecture_name'] && $logic_class['teacher_id'] == $me['id']) {
                    //     $logic_class_state = true;
                    // }
                    $logic_class_state = true;
                }
            }
            $school_id = 7;
            $this->load->model('major_model');
            // $major_exist = $this->major_model->check_major_name_exist_in_a_school($major_name,$school_id);
            // $major_exist = 1;

            $major = $this->teacher_model->findMajorById($teacher['id']);
            $major_name = $major['name'];
            $major_id = $major['id'];
            $major_exist = $this->major_model->check_major_name_exist_in_a_school($major_name, $school_id);
            $major_exist = 1;
            $data['major_name'] = $major_name;
            $data['major_exist'] = $major_exist;
            $data['students'] = $students;
            $data['major_id'] = $major_id;
            $data['teacher_name'] = $teacher_name;
            $data['teacher_number'] = $teacher_number;
            $data['teacher_exist'] = $teacher_exist;
            $data['teacher_state'] = $teacher_state;
            $data['logic_class_name'] = $logic_class_name;
            $data['logic_class_exist'] = $logic_class_exist;
            $data['lecture_exist'] = $lecture_exist;
            // $data['logic_class_state'] = $logic_class_state;
            $data['big_lecture_name'] = $big_lecture_name;
            $data['title'] = '批量添加预览';
            $this->load->view('admin_student/batch_add_preview', $data);
        }
    }

    /**
     * 检测并移除字符串中的bom
     * @param  [type] $text [description]
     * @return [type]       [description]
     */
    public function remove_utf8_bom($text)
    {
        $bom = pack('H*', 'EFBBBF');
        $text = preg_replace("/^$bom/", '', $text);
        return $text;
    }
    /**
     * 批量添加学生确认
     * @return [type] [description]
     */
    public function batch_add_confirm()
    {
        //加载需要的模型
        $this->load->model('teacher_model');
        $this->load->model('logic_class_model');
        $this->load->model('major_model');
        $this->load->model('user_model');
        $this->load->model('student_model');
    
        //读取数据
        $teacher_name = $this->input->post('teacher_name');
        $teacher_number = $this->input->post('teacher_number');
        $logic_class_name = $this->input->post('logic_class_name');
        $major_name = $this->input->post('major_name');
        $student_count = $this->input->post('student_count');
        $logic_class_state = $this->input->post('logic_class_state');
        $major = $this->major_model->get_by_major_name($major_name);
        $major_id = $major['id'];

        $teacher_exist = $this->teacher_model->teacher_number_exist($teacher_number);
        //如果班级不存在，错误
        //如果老师不存在，错误
        if (!$teacher_exist) {
            $data['error_message'] = '这个老师不存在';
            return $this->load->view('error', $data);
        }
        $teacher = $this->teacher_model->get_by_teacher_number($teacher_number);
        $this->load->model('teacher_model');
        $me = $this->teacher_model->get_by_username($this->session->userdata('r_username'));
        $logic_class_id = $this->logic_class_model->get_by_logic_class_name($logic_class_name);
        for ($i = 1; $i <= $student_count; $i++) {
            $student_name = $this->input->post('student_name_' . $i);
            $student_id = $this->input->post('student_id_' . $i);
            $student_exist = $this->student_model->student_id_exist($student_id);
            //如果不存在新建一个
            if (!$student_exist) {
                //新建一个用户
                $user = array(
                    'username' => $student_id,
                    'password' => md5($student_id),
                    'role_id' => 3,
                );

                if ($this->user_model->add($user)) {
                    $user_id = $this->db->insert_id();
                    //将学生写入数据库
                    $student = array(
                        'user_id' => $user_id,
                        'name' => $student_name,
                        'student_id' => $student_id,
                        'major_id' => $major_id,
                        'type' => 1,
                        'school_id' => 7
                    );
                    // $this->student_model->add($student);
                    $this->load->model('student_model');
                    if ($this->student_model->add($student)) {
                        //添加学生选课关系
                        $students = $this->student_model->get_by_student_id($student_id);
                        $student_in_logic_class = array(
                        'logic_class_id' => $logic_class_id['id'],
                        'student_id' => $students['id']
                    );
                        echo $students['id'];
                        $this->logic_class_model->AddStudentByLogicClass($student_in_logic_class);
                    }
                }
            }
            $student = $this->student_model->get_by_student_id($student_id);
        }

        $this->session->set_flashdata('message', '操作成功');
        redirect('admin_student/index');
    }


    public function cut_last_char($str)
    {
        return substr($str, 0, strlen($str) - 1);
    }
    /**
     * 查看一个学生的信息
     * @param  integer $id [description]
     * @return [type]      [description]
     */
    public function check($id = 0)
    {
        $this->load->model('student_model');
        $student = $this->student_model->get_by_id($id);
        if (!$student) {
            return $this->load->view('error');
        }

        $data['title'] = '查看学生';
        $data['student'] = $this->student_model->get_student_by_id($id);
        $data['relative_logic_classes'] = $this->student_model->get_relative_logic_classes($id);
        //var_dump($this->student_model->get_relative_logic_classes($id));

        $this->load->view('admin_student/check', $data);
    }

    /**
     * 删除一个学生
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function delete($id)
    {
        $this->load->model('student_model');
        if (!$this->student_model->get_by_id($id)) {
            $this->load->view('error');
        }
        if ($this->student_model->delete_cascade($id)) {
            $this->session->set_flashdata('message', '操作成功');
        } else {
            $this->session->set_flashdata('message', '存在相关引用，操作失败');
        }

        redirect('admin_student/index');
    }

    /**
     * 编辑一个学生的信息
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function edit($id)
    {
        if ($id) {
            //get方法
            $this->load->model('student_model');
            $student = $this->student_model->get_student_by_id($id);
            if (!$student) {
                return $this->load->view('error');
            }
            $data['title'] = '编辑学生信息';
            $data['student'] = $student;
            $this->load->model('major_model');
            $this->load->model('logic_class_model');
            $data['majors'] = $this->major_model->get_all();
            $data['logic_classes'] =  $this->logic_class_model->get_all();
           //  $data['logic_classes_select'] = $this->student_model->get_logic_classes_select($id);
            $this->load->view('admin_student/edit', $data);
        } else {
            //post方法
            //读取数据
            $this->load->model('student_model');

            $id = $this->input->post('id');
            $student = $this->student_model->get_by_id($id);
            if (!$student) {
                return $this->load->view('error');
            }
            if ($this->form_validation->run('admin_student/edit')) {
                $student_name = $this->input->post('student_name');
                $major_id = $this->input->post('major_id');
                $logic_classes = $this->input->post('logic_classes');

                //将模块写入数据库
                $student = array(
                    'name' => $student_name,
                    'major_id' => $major_id,
                );
                
                //数据库操作
                if ($this->student_model->delete_relative_logic_classes($id) &&
                    $this->student_model->add_relative_logic_classes($id, $logic_classes) &&
                    $this->student_model->update($id, $student)) {
                    $this->session->set_flashdata('message', '操作成功');
                    redirect('admin_student/index');
                } else {
                    $this->session->set_flashdata('message', '操作失败');
                    redirect('admin_student/index');
                }
            // } else {
            //     //验证失败
            //     //get
            //     $data['title'] = '编辑学生信息';
            //     $data['student'] = $student;
            //     $this->load->model('major_model');
            //     $this->load->model('logic_class_model');
            //     $logic_classes = $this->logic_class_model->get_all();
            //     $data['majors'] = $this->major_model->get_all();
            //     $data['logic_classes'] = $logic_classes;
            //     $this->load->view('admin_student/edit', $data);
            // }
        }
    }
}
     public function EditStudent(){
          //post方法
            //读取数据
            $this->load->model('student_model');
            $id = $this->input->post('id');
            echo $id;
            $student = $this->student_model->get_by_id($id);
            if (!$student) {
                return $this->load->view('error');
            }
                $student_name = $this->input->post('student_name');
                $major_id = $this->input->post('major_id');
                $logic_classes = $this->input->post('logic_classes');

                //将模块写入数据库
                $student = array(
                    'name' => $student_name,
                    'major_id' => $major_id,
                );
                
                //数据库操作
                if ($this->student_model->delete_relative_logic_classes($id) &&
                    $this->student_model->add_relative_logic_classes($id, $logic_classes) &&
                    $this->student_model->update($id, $student)) {
                    $this->session->set_flashdata('message', '操作成功');
                    redirect('admin_student/index');
                } else {
                    $this->session->set_flashdata('message', '操作失败');
                    redirect('admin_student/index');
            }
    }



    /**
     * 查询特定的某些同学
     * @return [type] [description]
     */
    public function search()
    {
        $major_id = $this->input->post('major_id');
        $logic_class_id = $this->input->post('logic_class_id');
        $student_type = $this->input->post('student_type');

        $this->load->model('student_model');
        // $students = $this->student_model->search($major_id, $class_id, $logic_class_id, $student_type);

        $students = $this->student_model->search($major_id, $logic_class_id, $student_type);
        $num = count($students);
        // for($i=0;$i<$num;$i++){
        // 	if(empty($studets[$i]['logic_class_name'])){
        // 		$students[$i]['logic_class_name']= '暂未选课';
        // 	}
        // }

        $this->load->model('major_model');
        // $this->load->model('class_model');
        $this->load->model('logic_class_model');
        $data['majors'] = $this->major_model->get_all();
        // $data['classes'] = $this->class_model->get_all();
        //		$data['logic_classes']=$this->logic_class_model->get_all_logic_classes();
        $data['logic_classes'] = $this->logic_class_model->get_all();
        $data['students'] = $students;

        $data['major_id_select'] = $major_id;
        $data['logic_class_id_select'] = $logic_class_id;
        $data['student_type_select'] = $student_type;
        $data['title'] = '查询';
        $this->load->view('admin_student/index', $data);
    }

    /**
     * 异步jquery验证
     * @return [type] [description]
     */
    public function check_student_id_exist()
    {
        $student_id = $this->input->post('student_id');
        $this->load->model('student_model');
        if ($this->student_model->student_id_exist($student_id)) {
            echo json_encode(false);
        } else {
            echo json_encode(true);
        }
    }

    /**
     * CI的表单回调函数验证
     * @param  [type] $str [description]
     * @return [type]      [description]
     */
    public function student_id_check($student_id)
    {
        $this->load->model('student_model');
        if ($this->student_model->student_id_exist($student_id)) {
            return false;
        } else {
            return true;
        }
    }
}
